﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.Forms;

namespace QLKDS.Forms
{
    public partial class frmDepartmentalAdd : XtraForm
    {
        #region Properties
        
        private DepartmentalManager departmentalManager;
        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
            this.txtName.Properties.MaxLength = 32;           
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            this.txtName.Text = String.Empty;
            this.txtDescription.Text = String.Empty;
           
            this.txtName.Focus();
        }
        #endregion

        #region Init form
        public frmDepartmentalAdd()
        {
            InitializeComponent();
            this.departmentalManager = new DepartmentalManager();
  
        }



        private void frmAuthorsAdd_Load(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
            this.SetMaxLengthOfTextBoxes();
           
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }
        private void txtName_Leave(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                this.dxepName.SetError(this.txtName, "Tên phòng ban không được để trống.", ErrorType.User1);
            }
        }


       
        #endregion

        #region Button_click
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var departmental = new Departmental();


            //validation name
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên phòng ban không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else if (this.departmentalManager.HasNameExisted(this.txtName.Text.Trim()) == true)
            {
                this.dxepName.SetError(this.txtName, "Tên phòng ban đã tồn tại trong hệ thống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepName.ClearErrors();
            }
            //end validation name

            // End of validation
            
            /*****************************************************************/
            // Save into database                  
        
            departmental.Name = this.txtName.Text.Trim();
            departmental.Description = this.txtDescription.Text.Trim();
            this.departmentalManager.CreateDepartmental(departmental);

            XtraMessageBox.Show("Dữ liệu thêm thành công");
            this.DialogResult = DialogResult.OK;
           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }

        #endregion

      

 

      

     
        


    }
}
